// pages/couponlist/couponlist.js
var app = getApp(), fetchApi = require("../../../utils/httpPost.js"),
common_Js = require("../../../component/common.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    couponstate:0,
    pageSize:10,
    pageIndex:1,
    couponList:[],
    banners:[],
    hasData:false,
    tabIndex:0,
    netWork:false,
    isLoading:true,
    couponNullText: ["暂无抵扣券", "暂无抵扣券"],
  },
  handleChange({ detail}) {
    console.log('handleChange',detail)
    var that = this
    that.data.couponstate = detail.index;
    this.data.pageIndex = 1;
    this.data.current = detail.index
    that.setData({
      current: detail.index,
      couponList:[],
      isLoading:true,
      tabIndex: that.data.couponstate 
    });
    if(detail.index == 0){
      that.onHttpInitUserMerCoupons()
    }else{
      that.onHttpInitUserCoupons()
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.onHttpInitUserMerCoupons();
  },
  /**
     * 获取页面数据的 http请求
     */
  onHttpInitUserCoupons: function () {
    var that = this
    fetchApi.httpPost('/coupon/user/list', {
      // state: that.data.couponstate,
      pageSize: that.data.pageSize,
      pageIndex: that.data.pageIndex
    }, function (res) {
      console.log("优惠券列表数据", res)
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          that.data.pageIndex = that.data.pageIndex + 1;
          var coupons = res.data.data.coupons;
          console.log('coupons',coupons)
          if(coupons.length > 0){
            console.log('coupons',coupons)
            for (var index in coupons) {
              that.data.couponList.splice(that.data.couponList.length, 0, coupons[index]);
            }
            that.setData({
              hasData: true,
              couponList: that.data.couponList
            })
          }
        }
      }
      that.setData({ netWork: true, isLoading: false, })
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    },function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, })
    })
  },
  /**
     * 获取页面数据的 http请求
     */
  onHttpInitUserMerCoupons: function () {
    var that = this
    fetchApi.httpPost('/mer/coupon/user/list', {
      pageSize: that.data.pageSize,
      pageIndex: that.data.pageIndex
    }, function (res) {
      console.log("优惠券列表数据", res)
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          that.data.pageIndex = that.data.pageIndex + 1;
          var coupons = res.data.data.coupons;
          console.log('coupons',coupons)
          if(coupons.length > 0){
            console.log('coupons',coupons)
            for (var index in coupons) {
              that.data.couponList.splice(that.data.couponList.length, 0, coupons[index]);
            }
            that.setData({
              hasData: true,
              couponList: that.data.couponList
            })
          }
        }
      }
      that.setData({ netWork: true, isLoading: false, })
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    },function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, })
    })
  },
  pushToHome:function(){
    wx.switchTab({ url: '/pages/home/home', })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.pageIndex = 1;
    this.setData({
      couponList:[],
      isLoading:true,
    });
    if(this.data.current == 0){
      this.onHttpInitUserMerCoupons()
    }else{
      this.onHttpInitUserCoupons()
    }
  },
  /**
   * 页面上拉触底事件的处理函数    加载更多
   */
  onReachBottom: function () {
    if(this.data.current == 0){
      this.onHttpInitUserMerCoupons()
    }else{
      this.onHttpInitUserCoupons()
    }
  },
})